<?php
namespace app\api\controllers;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
use app\api\Constant;
use app\common\utils\Wechat;
use app\models\GkyPost;
use app\models\GkyWeFans;
use Overtrue\Wechat\Server;
use yii\web\Controller;
use app\api\Response;
use yii\helpers\ArrayHelper;
use app\models\GkyUser;
use app\models\GkyPostReply;
/**
 * Description of PostController
 *
 * @author Administrator
 */
class PostController extends Controller{
    /**
     * 创建帖子
     * @return type
     */
    public function actionCreatePost(){
        $data = \Yii::$app->request->get();
        $post=new GkyPost();
        $post->user_id=$data['user_id'];
        $post->content=$data['content'];
        $post->createtime=$data['createtime'];
         $result = [];
        if($post->save()){
              $result['status'] = 0;
        }else{
              $result['status'] = 1;
        }
       Response::setHeader();
       return json_encode($result);
    }
    
    /**
     * 创建回复帖子
     * @return type
     */
    public function actionCreateReply(){
        $data = \Yii::$app->request->get();
        $post=new GkyPostReply();
        
        $post->post_id=$data['post_id'];
        $post->user_id=$data['user_id'];
        $post->content=$data['content'];
        $post->createtime=$data['createtime'];
        
         $result = [];
        if($post->save()){
            $this->sendReply($post);
            $result['status'] = 0;
        }else{
            $result['status'] = 1;
        }
       Response::setHeader();
       return json_encode($result);
    }
    
     public function actionReplyList($post_id){
         $list =GkyPostReply::find()
                 ->where(['post_id'=>$post_id])
                 ->orderBy(['createtime' => SORT_DESC])
                 ->all();
         
        $results = [];
        foreach($list as $model){
            $result['id'] = $model->id;
            $result['user_id'] = $model->user_id;
            $result['username'] = GkyWeFans::find()->where(['id'=>$model->user_id])->one()->nickname;
            $result['content'] = $model->content;
            $result['createtime'] = $model->createtime;
            $results[] = $result;
        }
        Response::setHeader();
        echo json_encode(ArrayHelper::toArray($results));
     }
    /**
     * 根据用户id获取自己发的帖子list
     * @param type $user_id
     * @return type
     */
    
    public function actionPostList($pageNow, $pageSize){
        $list = GkyPost::findPostList($pageNow, $pageSize);
        Response::setHeader();
        echo json_encode(ArrayHelper::toArray($list));
    }
    
    public function actionGetPostById($post_id){
        $post=  GkyPost::find()->where(['id'=>$post_id])
                ->one();
        $result['post_id']=$post->id;
        $result['user_id']=$post->user_id;
        $result['username'] = GkyWeFans::find()->where(['id'=>$post->user_id])->one()->nickname;
        $result['content']=$post->content;
        $result['createtime']=$post->createtime;

        Response::setHeader();
        echo json_encode(ArrayHelper::toArray($result));
    }

    public function sendReply($reply){

        $user = GkyWeFans::findOne($reply->user_id);
        $post = GkyPost::findOne($reply->post_id);

        $options = array(
            'token'=>'gky', //填写你设定的key
            'appid'=>Constant::$WE_APPID, //填写高级调用功能的app id, 请在微信开发模式后台查询
            'appsecret'=>Constant::$WE_SECRET //填写高级调用功能的密钥
        );
        $wechat = new Wechat($options);
        \Yii::info('openid:'.$user->openid,'weixin');
        $message = [
            "touser" => $user->openid.'',
            "msgtype" => "text",  //根据信息类型，选择下面对应的信息结构体

            "text" => array(
                "content" => $user->nickname.' 回复了您的【'.$post->content.'】帖子:'.$reply->content
            ),
        ];
        $result = $wechat->sendCustomMessage($message);
        \Yii::info('sendMessage:'.json_encode($result),'weixin');
    }
    
}
